package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.CreateNftInfo;
import com.zjgsu.nftplatform.pojo.CreateUserInfo;
import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2022/3/5 10:14
 * @description
 */
@Api(tags = "用户")
@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("获取个人信息")
    @GetMapping("/userInfo")
    public Result userInfo(@ApiParam("用户id") @RequestParam(required = false) Long userId) {
        try {
            return new Result(ResultCode.SUCCESS, userService.userInfo(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/settingInfo")
    public Result settingInfo(@ApiParam("用户头像") @RequestParam String img, @ApiParam("用户昵称") @RequestParam String name, @ApiParam("用户签名") @RequestParam String signature, @ApiParam("用户手机号") @RequestParam String phone, @ApiParam("实名认证情况") @RequestParam Boolean certification) {
        try {
            return new Result(ResultCode.SUCCESS, userService.settingInfo(img, name, signature, phone, certification));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("创建nft")
    @PostMapping("/nftCreate")
    public Result nftCreate(@ApiParam("nft信息") @RequestBody CreateNftInfo info) {
        try {
            return new Result(ResultCode.SUCCESS, userService.nftCreate(info));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看NFT")
    @GetMapping("/wareView")
    public Result wareView(@ApiParam("nftID") @RequestParam Long nftId) {
        try {
            return new Result(ResultCode.SUCCESS, userService.nftView(nftId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("修改NFT")
    @PostMapping("/wareUpdate")
    public Result wareUpdate(@ApiParam("nftId") @RequestParam Long nftId, @ApiParam("封面img") @RequestParam(required = false) String img, @ApiParam("商品名") @RequestParam(required = false) String name, @ApiParam("商品描述") @RequestParam(required = false) String description, @ApiParam("属性") @RequestParam(required = false) String attribute) {
        try {
            userService.nftUpdate(nftId, img, name, description, attribute);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("查看用户nft")
    @GetMapping("/nft")
    public Result getNft(@ApiParam("用户id") @RequestParam(required = false) Long userId,
                         @ApiParam("页号") @RequestParam Integer pageNum,
                         @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.getNft(userId, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("我关注的作品")
    @GetMapping("/artFollow")
    public Result artFollow(@ApiParam("pageNum") @RequestParam Long pageNum, @ApiParam("pageSize") @RequestParam Long pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.artFollow(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("我关注的创作者")
    @GetMapping("/creatorFollow")
    public Result creatorFollow(@ApiParam("pageNum") @RequestParam Long pageNum, @ApiParam("pageSize") @RequestParam Long pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.creatorFollow(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("我的粉丝")
    @GetMapping("/fans")
    public Result fans(@ApiParam("pageNum") @RequestParam Long pageNum, @ApiParam("pageSize") @RequestParam Long pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.fans(pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("用户名密码登录")
    @PostMapping("/passwordLogin")
    public Result passwordLogin(@ApiParam("用户名") @RequestParam String username, @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, userService.passwordLogin(username, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED_LOGIN, e.getMessage());
        }
    }

    @ApiOperation("注册")
    @PostMapping("/create")
    public Result create(@ApiParam("用户信息") @RequestBody CreateUserInfo info) {
        try {
            userService.createUser(info);
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("用户认证")
    @PostMapping("/certificate")
    public Result certificate() {
        try {
            userService.certificate();
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("用户商品")
    @GetMapping("/goods")
    public Result copyright(@ApiParam("用户id") @RequestParam(required = false) Long userId,
                            @ApiParam("页号") @RequestParam Integer pageNum,
                            @ApiParam("页面大小") @RequestParam Integer pageSize) {
        try {
            return new Result(ResultCode.SUCCESS, userService.userGoods(userId, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}